//import { mergeSameItem } from "@/utils/cartFn.js";
import {AddCart} from '@/api/order'

export let handleItem = (item, isPackage = false)=>{
  console.log(item)
  if (isPackage == true) {
    let res = item.itemCount > 0 ? true : false;
    if (res == false) return res;
  }
  let unitId = "";
  if (item.type == 3 || item.type == 5) {
    unitId = isPackage == false ? item.baseUnit.unitId : item.unit.unitId;
  } else {
   unitId = item.baseUnitId ?? "";
  }
  let qty = isPackage == true ? item.itemCount : item.qty;
  let obj = {
    productId: item.productId,
    unitId,
    type: item.type,
    qty: qty ?? 0,
    state: item.type,
    configIds: [],
    items: [],
    comment: "",
    detailId:""
  };
  // 备注
  if (item.comment) {
    obj.comment = item.comment;
  }
  // detailId
  if (item.detailId) {
    obj.detailId = item.detailId;
  }
  if (item.type == 3 && isPackage == false) {
    for (let pitem of item.packages) {
      if (pitem.items.length <= 0) continue;
      pitem.items.forEach((pi) => {
        let res = handleItem(pi, true);
        if (res != false) {
          obj.items.push(res);
        }
      });
    }
  } else if (item.type == 5) {
    for (let fitem of item.foodGroups) {
      for (let i of fitem.items) {
        if (i.isDefault == true) {
          obj.configIds.push(i.itemId);
        }
      }
    }
  } else if (item.type == 0 && item?.sku?.length > 0) {
    for (let sitem of item.sku) {
      if (sitem.isDefault == true) {
        obj.configIds.push(sitem.skuId);
      }
    }
  }
  return obj;
}

export let testHander = (item, isPackage = false)=>{
  
  console.log(item)
  if (isPackage == true) {
    let res = item.itemCount > 0 ? true : false;
    if (res == false) return res;
  }
  // let unitId = "";
  // if (item.type == 3 || item.type == 5) {
  //   unitId = isPackage == false ? item.baseUnit.unitId : item.unit.unitId;
  // } else {
  //  unitId = item.baseUnitId ?? "";
  // }
  let qty = isPackage == true ? item.itemCount : item.qty;
  let obj = {
    productId: item.productId,
    unitId:"",
    type: item.type,
    qty: qty ?? 0,
    state: item.type,
    configIds: [],
    items: [],
    comment: "",
    detailId:""
  };
  // 备注
  if (item.comment) {
    obj.comment = item.comment;
  }
  // detailId
  if (item.detailId) {
    obj.detailId = item.detailId;
  }
  if (item.type == 3 && isPackage == false) {
    for (let pitem of item.packages) {
        let res = testHander(pitem, true);
        if (res != false) {
          obj.items.push(res);
        }
    }
  } else if (item.type == 5) {
    for (let fitem of item.foodGroups) {
      obj.configIds.push(fitem.itemId);
    }
  } else if (item.type == 0 && item?.sku?.length > 0) {
    for (let sitem of item.sku) {
      if (sitem.isDefault == true) {
        obj.configIds.push(sitem.skuId);
      }
    }
  }
  return obj;
}

export default {
  state: {
    shoppingList: [],
    timer: null
  },
  getters: {
    mapShoppingList({ shoppingList }) {
      //let arr = mergeSameItem(shoppingList);
      return shoppingList;
    },
  },
  mutations: {
    /**
     * @description 设置整单
     */
    setShoppintList(state, { list }) {
      state.shoppingList = list;
    },
    /**
     *@description 清空商品列表
     */
    clearshoppingList(state) {
      state.shoppingList = [];
    },
    /**
     * @description 修改商品
     */
    modifyPro({ shoppingList }, { item }) {
      let obj = shoppingList.find((sitem) => {
        return sitem == item;
      });
      if (obj != undefined) {
        obj = item;
      }
    },
    modifyProNew({ shoppingList }, { cur, replace }) {
      let index = shoppingList.findIndex((item) => {
        return item == cur;
      });
      if (index != -1) {
        // shoppingList[index] = replace;
        shoppingList.splice(index, 1, replace);
      }
    },
    modifyProCustom({ shoppingList }, { fn }) {
      fn(shoppingList);
    },
    /**
     * @description 添加商品
     */
    increasePro(state, list) {
      state.shoppingList = [];
      state.shoppingList.push(...list);
    },
    /**
     * @description 删除商品
     */
    decreasePro(state, list) {
      state.shoppingList = [];
      state.shoppingList.push(...list);
    },
    /**
     * @description 添加商品
     */
     increaseProCart({ shoppingList }, { item }) {
      shoppingList.push(item);
    },
    /**
     * @description 删除商品
     */
    decreaseProCart({ shoppingList }, { item }) {
      let index = shoppingList.findIndex((shopItem) => {
        return shopItem.detailId == item.detailId;
      });
      if (index != -1) {
        shoppingList.splice(index, 1);
      }
    },
  },
  actions:{
    async increasePro({commit,rootState},{item}){
      item.qty = 1;
      let items = []
      items.push(handleItem(item))
      //Vue.set(item,'qty',1)
      
      let param = {
        tableId: rootState.tableInfo.tableId,
        peopleCount: rootState.tableInfo.num,
        comment: "",
        items: items
      }
      let { returnData = false }  = await AddCart(param)
      if(returnData == false) return
      commit('increasePro',returnData.details)
    },
    async decreasePro({commit,rootState},{item}){
      let items = [];
      item.qty = -1;
      //Vue.set(item,'qty',-1)
      items.push(handleItem(item))
      let param = {
        tableId: rootState.tableInfo.tableId,
        peopleCount: rootState.tableInfo.num,
        comment: "",
        items: items
      }
      let { returnData = false }  = await AddCart(param)
      if(returnData == false) return
      commit('decreasePro',returnData.details)
    },
    async increaseProCart({commit,rootState},{item}){
      item.qty = 1;
      let items = []
      // item.items = item.packages
      // delete item.packages
      // items.push(item)
      //Vue.set(item,'qty',1)
      items.push(testHander(item))
      let param = {
        tableId: rootState.tableInfo.tableId,
        peopleCount: rootState.tableInfo.num,
        comment: "",
        items: items
      }
      let { returnData = false }  = await AddCart(param)
      if(returnData == false) return
      commit('increasePro',returnData.details)
    },
    async decreaseProCart({commit,rootState},{item}){
      let items = [];
      item.qty = -1;
      //Vue.set(item,'qty',-1)
      items.push(item)
      let param = {
        tableId: rootState.tableInfo.tableId,
        peopleCount: rootState.tableInfo.num,
        comment: "",
        items: items
      }
      let { returnData = false }  = await AddCart(param)
      if(returnData == false) return
      commit('decreasePro',returnData.details)
    }
  }
};
